﻿using Dapper;
using MediatR;
using MySql.Data.MySqlClient;
using System.Text;
using Warehouse.Api.Read.AGCation.Command;
using Warehouse.Api.Read.Dtos;
using Warehouse.ErrorCode;
using Warehouse.Infrastructrue.Implenment;

namespace Warehouse.Api.Read.AGCation.Hanlder
{
    public class InboundCommandHanlder : IRequestHandler<InboundCommand, ApiPaging<InboundDto>>
    {
        DbContext db;
        ILogger<InboundCommandHanlder> logger;

        public InboundCommandHanlder(DbContext db, ILogger<InboundCommandHanlder> logger)
        {
            this.db = db;
            this.logger = logger;
        }
        /// <summary>
        /// 统计报错信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public  async Task<ApiPaging<InboundDto>> Handle(InboundCommand request, CancellationToken cancellationToken)
        {    
             ApiPaging<InboundDto>result = new ApiPaging<InboundDto>();
            try
            {
                StringBuilder sqlbuilder = new StringBuilder();
                StringBuilder countsql = new StringBuilder();

                sqlbuilder.Append(@" SELECT 
	                        s.AssociatedOrderNumber,
		                    s.IncomingQuantity,
		                    s.PreparedTime,
		                    s.Price,
		                    t.STName,
		 d.DepotName,
		 d.DepotCode,
		 l.LocationName,
		 p.ProductName,
		 p.Specifications
		 FROM Stocking s JOIN Stocking_Type t
		 ON s.STId = t.STId JOIN LocationModel l ON s.StorageId = l.LocationId
		 join DepotModel d  ON s.StorageId = d.DepotId JOIN  ProductModel p ON s.ProductId = p.ProductId
WHERE 0= 0 ");
                countsql.Append(@" SELECT COUNT(s.id) FROM Stocking s JOIN Stocking_Type t
		 ON s.STId = t.STId JOIN LocationModel l ON s.StorageId = l.LocationId
		 join DepotModel d  ON s.StorageId = d.DepotId JOIN  ProductModel p ON s.ProductId = p.ProductId
WHERE 0= 0 ");
                var parmenters = new DynamicParameters();
                sqlbuilder.Append("limit @pagesize offset @offset");
                parmenters.Add("pagesize", request.PageSize);
                parmenters.Add("offset", (request.PageIndex - 1) * request.PageSize);
                using var conn = new MySqlConnection("server=120.27.210.76;uid=wms_user;pwd=MyPassword123!;database=warehousemanagement;CharSet=utf8mb4;");
                await conn.OpenAsync(cancellationToken);
                var data = await conn.QueryAsync<InboundDto>(sqlbuilder.ToString(), parmenters);
                var total = await conn.ExecuteScalarAsync<int>(countsql.ToString(), parmenters);
                if (request.PageSize <= 0)
                {
                    result.Msg = "显示数量不能小于等于0";
                    result.Code = ApiEume.Error;
                    return result;
                }
                result.Msg = "显示成功";
                result.Code = ApiEume.Success;
                result.PageData = data.ToList();
                result.TotaCount = total;
                result.PageCount = (int)Math.Ceiling((double)total / request.PageSize);
            }
            catch (Exception ex)
            {

            }
            return result;
        }
    }
}
